#ifndef TOOLPARTS_H
#define TOOLPARTS_H

#include <QColor>
#include <QTableWidget>
#include <QTableWidgetItem>

#include "../cellEdit/cellEdit.h"

#include "defines.h"

#define  DEFSTEP_L	0.01
#define  DEFSTEP_F	0.01
#define  DEFSTEP_I	0.001

#define	 DEFMIN_L	0.0
#define  DEFMAX_L	9999.0

#define	 DEFMIN_F	0.0
#define  DEFMAX_F	99999.0

#define	 DEFMIN_I	0.0
#define  DEFMAX_I	9999.0

class ToolParts : public QTableWidget {
 Q_OBJECT
public:
  ToolParts(QWidget *parent = 0);

  void setColor(QColor color, int column);
  
  void setEditPart(int num);
  int editPart() { return curPart; };

  void setL(double newVal, int column);
  void setCurL(double newVal) { setL(newVal, curPart); };
  double l(int column);
  double curL() { return l(curPart); };

  void setF(double newVal, int column);
  void setCurF(double newVal) { setF(newVal, curPart); };
  double f(int column);
  double curF() { return f(curPart); };

  void setI(double newVal, int column);
  void setCurI(double newVal) { setI(newVal, curPart); };
  double i(int column);
  double curI() { return i(curPart); };

  void setIr(double newVal, int column);
  void setCurIr(double newVal) { setIr(newVal, curPart); };
  double ir(int column);
  double curIr() { return ir(curPart); };

  bool isPartNull(int num);

  int partCount();

signals:
  void changedL(double newVal);
  void changedF(double newVal);
  void changedI(double newVal);
  void changedIr(double newVal);
  void changedCurPart(int newCurPart);
  void newMessage(QString mes);
  void changedValue();

public slots:
  void changeCurPart(int newCurPart) { activePart(0, newCurPart); };
  void setFocusF();

protected slots:
  void changeL(double val, bool isChange = true);
  void changeF(double val, bool isChange = true);
  void changeI(double val, bool isChange = true);
  void changeIr(double val, bool isChange = true);
  void activePart(int row, int col);
  void genMessageL(double val);
  void genMessageF(double val, double fullFParts);
  void genMessageI(double val);
  void genMessageIr(double val);

protected:
  QColor deActiveColor;
  QColor activeColor;
  QColor hightColor;

  CellEdit * insEdit;

  //Текущий участок
  int curPart;

  void resetEdit();

  QTableWidgetItem * insItem;

  //Убирает незначащие цифры после запятой
  double toPrec(double num) { double d = fmod(num, 0.01); return  num - d + double(rint(d * 1000))/1000; };

private:
  void editView(bool newVal, int row, int col);

}; //class ToolParts : public QTableWidget

#endif
